﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zxb.DayWork.Logic;
using System.Data;
using System.Xml;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace Zxb.OneTimeWork
{
    public class CaseModel
    {
        public string CaseName;
        public int CaseRoomID;
        public string CaseRoomName;
        public int CaseStyleID;
        public string CaseStyleName;
        public string Designer;
        public int CityID;
        public string CityName;
        public long EstateID;
        public string EstateName;
        public decimal Area;
        public int Price;
        public string PicUrl;
        public string SmallPicUrl;
        public int PicCount;
        public DateTime AddedTime;
        public DateTime LastModifiedTime;
        public int IsAutoGen;
        public int IsValid;
        public int weike_BranchID;
        public int weike_CompanyID;
        public int weike_DepartID;
        public int WeiKe_UserID;
        public int CaseHallID;
        public string CaseHallName;
        public int CaseToiletID;
        public string CaseToiletName;
        public int RoomDataID;
        public string RoomDataTitle;

        //public static void InsertCase(CaseModel model)
        //{
        //    string sql = string.Format("insert into [test1].[dbo].[Cases] (CaseName,CaseRoomID,CaseRoomName,CaseStyleID,CaseStyleName,Designer,CityID,CityName,RealEstateID,RealEstate,Area,Price,PicUrl,SmallPicUrl,PicCount,AddedTime,LastModifiedTime,IsValid,weike_BranchID,weike_CompanyID,weike_DepartID,WeiKe_UserID,CaseHallID,CaseHallName,CaseToiletID,CaseToiletName) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},'{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}')",
        //        model.CaseName, model.CaseRoomID, model.CaseRoomName, model.CaseStyleID, model.CaseStyleName, 
        //        model.Designer, model.CityID, model.CityName, model.EstateID, model.EstateName, 
        //        model.Area, model.Price, model.PicUrl, model.AddedTime, model.LastModifiedTime, 
        //        model.IsValid, model.weike_BranchID, model.weike_CompanyID, model.weike_DepartID, model.WeiKe_UserID, 
        //        model.CaseHallID, model.CaseHallName, model.CaseToiletID, model.CaseToiletName);

        //    DAL.SimpleInsert(DB.Local, sql);
        //}

        public CaseModel()
        {

        }
        public static CaseModel GetCaseModel(FakeCase fcase, Dictionary<string, string> dic_estID_estName)
        {
            Random rm = new Random(DateTime.Now.Millisecond);

            RoomData r = fcase.Room;

            CaseModel casemodel = new CaseModel();
            casemodel.AddedTime = DateTime.Now;
            casemodel.Area = r.Area;
            casemodel.CaseHallID = r.HallNum;
            casemodel.CaseHallName = RoomConstant.Halls[r.HallNum - 1];

            casemodel.CaseRoomID = r.RoomNum;
            casemodel.CaseRoomName = RoomConstant.Rooms[r.RoomNum - 1];
            casemodel.CaseStyleID = fcase.Pic.CaseStyleID;
            casemodel.CaseStyleName = RoomConstant.Styles[fcase.Pic.CaseStyleID - 1];
            casemodel.CaseToiletID = r.ToiletNum;
            casemodel.CaseToiletName = RoomConstant.Toilets[r.ToiletNum - 1];
            casemodel.CityID = r.CityID;
            casemodel.CityName = r.CityName;

            casemodel.EstateID = r.EstateID;
            casemodel.EstateName = dic_estID_estName.ContainsKey(r.EstateID.ToString()) ? dic_estID_estName[r.EstateID.ToString()] : "";
            casemodel.IsAutoGen = 1;
            casemodel.IsValid = 1;
            casemodel.LastModifiedTime = DateTime.Now;
            casemodel.PicCount = 4;
            casemodel.PicUrl = fcase.Pic.PicUrl;
            casemodel.Price = rm.Next(2, 21) * 10000;
            casemodel.SmallPicUrl = fcase.Pic.SmallPicUrl;
            casemodel.weike_BranchID = -1;
            casemodel.weike_CompanyID = -1;
            casemodel.weike_DepartID = -1;
            CityConfig cityconf = CityConfig.CityList.FirstOrDefault(x => x.CityID == r.CityID);
            casemodel.WeiKe_UserID = cityconf == null ? 0 : cityconf.WeiKe_UserID;
            casemodel.Designer = cityconf == null ? "" : cityconf.SoufunName;

            casemodel.RoomDataID = r.RoomDataID;
            casemodel.RoomDataTitle = r.RoomDataTitle;

            casemodel.CaseName = string.Format("{0}-{1}-{2}", casemodel.EstateName, casemodel.CaseStyleName, casemodel.CaseRoomName);
            return casemodel;
        }
        public static int InsertCase(CaseModel model)
        {
            //Database db = DB.Local;
            //string sql = "insert into [test1].[dbo].[Cases] (CaseName,CaseRoomID,CaseRoomName,CaseStyleID,CaseStyleName,Designer,CityID,CityName,RealEstateID,RealEstate,Area,Price,PicUrl,SmallPicUrl,PicCount,AddedTime,LastModifiedTime,IsValid,weike_BranchID,weike_CompanyID,weike_DepartID,WeiKe_UserID,CaseHallID,CaseHallName,CaseToiletID,CaseToiletName,IsAutoGen,RoomDataID,RoomDataTitle) values (@CaseName,@CaseRoomID,@CaseRoomName,@CaseStyleID,@CaseStyleName,@Designer,@CityID,@CityName,@RealEstateID,@RealEstate,@Area,@Price,@PicUrl,@SmallPicUrl,@PicCount,@AddedTime,@LastModifiedTime,@IsValid,@weike_BranchID,@weike_CompanyID,@weike_DepartID,@WeiKe_UserID,@CaseHallID,@CaseHallName,@CaseToiletID,@CaseToiletName,@IsAutoGen,@RoomDataID,@RoomDataTitle);SELECT @@identity";
            Database db = DB.TaskWriter;
            string sql = "insert into [dbo].[Cases] (CaseName,CaseRoomID,CaseRoomName,CaseStyleID,CaseStyleName,Designer,CityID,CityName,RealEstateID,RealEstate,Area,Price,PicUrl,SmallPicUrl,PicCount,AddedTime,LastModifiedTime,IsValid,weike_BranchID,weike_CompanyID,weike_DepartID,WeiKe_UserID,CaseHallID,CaseHallName,CaseToiletID,CaseToiletName,IsAutoGen,RoomDataID,RoomDataTitle) values (@CaseName,@CaseRoomID,@CaseRoomName,@CaseStyleID,@CaseStyleName,@Designer,@CityID,@CityName,@RealEstateID,@RealEstate,@Area,@Price,@PicUrl,@SmallPicUrl,@PicCount,@AddedTime,@LastModifiedTime,@IsValid,@weike_BranchID,@weike_CompanyID,@weike_DepartID,@WeiKe_UserID,@CaseHallID,@CaseHallName,@CaseToiletID,@CaseToiletName,@IsAutoGen,@RoomDataID,@RoomDataTitle);SELECT @@identity";
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            db.AddInParameter(dbCommand, "@CaseName", DbType.String, model.CaseName);
            db.AddInParameter(dbCommand, "@CaseRoomID", DbType.Int16, model.CaseRoomID);
            db.AddInParameter(dbCommand, "@CaseRoomName", DbType.String, model.CaseRoomName);
            db.AddInParameter(dbCommand, "@CaseStyleID", DbType.Int16, model.CaseStyleID);
            db.AddInParameter(dbCommand, "@CaseStyleName", DbType.String, model.CaseStyleName);

            db.AddInParameter(dbCommand, "@Designer", DbType.String, model.Designer);
            db.AddInParameter(dbCommand, "@CityID", DbType.Int32, model.CityID);
            db.AddInParameter(dbCommand, "@CityName", DbType.String, model.CityName);
            db.AddInParameter(dbCommand, "@RealEstateID", DbType.Int64, model.EstateID);
            db.AddInParameter(dbCommand, "@RealEstate", DbType.String, model.EstateName);

            db.AddInParameter(dbCommand, "@Area", DbType.Decimal, model.Area);
            db.AddInParameter(dbCommand, "@Price", DbType.Int32, model.Price);
            db.AddInParameter(dbCommand, "@PicUrl", DbType.String, model.PicUrl);
            db.AddInParameter(dbCommand, "@SmallPicUrl", DbType.String, model.SmallPicUrl);
            db.AddInParameter(dbCommand, "@PicCount", DbType.Int32, model.PicCount);

            db.AddInParameter(dbCommand, "@AddedTime", DbType.DateTime, model.AddedTime);
            db.AddInParameter(dbCommand, "@LastModifiedTime", DbType.DateTime, model.LastModifiedTime);
            db.AddInParameter(dbCommand, "@IsValid", DbType.Int16, model.IsValid);
            db.AddInParameter(dbCommand, "@weike_BranchID", DbType.Int32, model.weike_BranchID);
            db.AddInParameter(dbCommand, "@weike_CompanyID", DbType.Int32, model.weike_CompanyID);

            db.AddInParameter(dbCommand, "@weike_DepartID", DbType.Int32, model.weike_DepartID);
            db.AddInParameter(dbCommand, "@WeiKe_UserID", DbType.Int32, model.WeiKe_UserID);
            db.AddInParameter(dbCommand, "@CaseHallID", DbType.Int16, model.CaseHallID);
            db.AddInParameter(dbCommand, "@CaseHallName", DbType.String, model.CaseHallName);
            db.AddInParameter(dbCommand, "@CaseToiletID", DbType.Int16, model.CaseToiletID);

            db.AddInParameter(dbCommand, "@CaseToiletName", DbType.String, model.CaseToiletName);
            db.AddInParameter(dbCommand, "@IsAutoGen", DbType.Int32, model.IsAutoGen);
            db.AddInParameter(dbCommand, "@RoomDataID", DbType.Int32, model.RoomDataID);
            db.AddInParameter(dbCommand, "@RoomDataTitle", DbType.String, model.RoomDataTitle);
            //return db.ExecuteNonQuery(dbCommand);
            object id = db.ExecuteScalar(dbCommand);
            int result = 0;
            int.TryParse(id.ToString(), out result);
            return result;
        }

        public static void InsertTest()
        {
            CaseModel cm = new CaseModel();
            cm.AddedTime = DateTime.Now;
            cm.Area = 80;
            cm.CaseHallID = 2;
            cm.CaseHallName = "2";
            cm.CaseName = "t";
            cm.CaseRoomID = 3;
            cm.CaseRoomName = "3";
            cm.CaseStyleID = 4;
            cm.CaseStyleName = "4";
            cm.CaseToiletID = 1;
            cm.CaseToiletName = "1";
            cm.CityID = 203;
            cm.CityName = "203";
            cm.Designer = "gj";
            cm.EstateID = 12345678L;
            cm.EstateName = "qw";
            cm.IsAutoGen = 1;
            cm.IsValid = 1;
            cm.LastModifiedTime = DateTime.Now;
            cm.PicCount = 4;
            cm.PicUrl = "hht";
            cm.Price = 90000;
            cm.SmallPicUrl = "http";
            cm.weike_BranchID = -1;
            cm.weike_CompanyID = -1;
            cm.weike_DepartID = -1;
            cm.WeiKe_UserID = 12345;
            cm.RoomDataID = 999;
            cm.RoomDataTitle = "huxing";

            InsertCase(cm);
        }
    }
}
